// components/amount/index.js
const app = getApp()
const service = require('../../service/port.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getUserInfo: function (e) {
      console.log(e);
      if (e.detail.userInfo) {
        app.globalData.userInfo = e.detail
        wx.checkSession({
          success: function (res) {
            console.log("处于登录态");
            service.login({
              code: wx.getStorageSync('code').code,
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              id:wx.getStorageSync('qrcodeId') || 0
            }, function (result) {
              console.log(result);
              wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
              })
              wx.setStorageSync("userInfo", result.data);
              service.getdiamondNum(result.data.id, res => { //获取钻石数量
                //console.log(res)
                wx.setStorageSync("diamondNum", res.data.scort);
    
              })
            })
          },
          fail: function (res) {
            console.log("需要重新登录");
            wx.login({
              success: (res1) => {
                wx.setStorageSync("code", res1);
                service.login({
                  code: res1.code,
                  encryptedData: e.detail.encryptedData,
                  iv: e.detail.iv,
                  id:wx.getStorageSync('qrcodeId') || 0
                }, function (result) {
                  console.log(result);
                  wx.showToast({
                    title: '登录成功',
                    icon: 'success',
                    duration: 2000
                  })
                  wx.setStorageSync("userInfo", result.data);
                  service.getdiamondNum(result.data.id, res => { //获取钻石数量
                    //console.log(res)
                    wx.setStorageSync("diamondNum", res.data.scort);
        
                  })
                })
              }
            })
          }
        })
        
        this.setData({
          // userInfo: e.detail.userInfo,
          hasUserInfo: true
        })
        this.triggerEvent('loginSuccess')
      }

    },
  }
})